package com.example.ordersystem.controller;

import com.example.ordersystem.entity.ShoppingCart;
import com.example.ordersystem.entity.vo.ResultMessage;
import com.example.ordersystem.service.ShoppingCartService;
import com.example.ordersystem.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("order/shoppingCart")
@CrossOrigin
public class ShoppingCartController {

    @Autowired
    ResultUtil resultUtil;

    @Autowired
    ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     */
    @PutMapping("add")
    public ResultMessage add(@RequestBody Map<String,String> map)
    {
        ShoppingCart shoppingCart=new ShoppingCart();
        shoppingCart.setFood_id(map.get("food_id"));
        shoppingCart.setFood_num(map.get("food_num"));
        shoppingCart.setUser_id(map.get("user_id"));
        shoppingCartService.add(shoppingCart);
        return resultUtil.success("添加成功");
    }

    /**
     * 根据用户id查询购物车信息
     * @param id 用户id
     * @return 购物车信息
     */
    @GetMapping("select/{id}")
    public ResultMessage select(@PathVariable String id)
    {
        return resultUtil.data(shoppingCartService.select(id));
    }
    /**
     * 删除购物车信息
     */
    @DeleteMapping("{id}")
    public ResultMessage del(@PathVariable String id)
    {
        shoppingCartService.del(id);
        return resultUtil.success("删除成功");
    }
}
